// pages/hot_book/hot_book.js
let util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        detail:[],
        pageNumber:1,
        readMoreFlag: false,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    //获取书籍列表
    getList: function(){
        const that = this;
        let pageNumber = this.data.pageNumber;
        let oldData = this.data.detail;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        this.setData({
            readMoreFlag:false
        });
        util.wxAjax("glBook/moreBooks","post",{
            pageNum: pageNumber,
            adminid:userInfo.smAdmin.id
        },function(res){
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
            wx.hideLoading();
            var data = res.data;
            if(!!data.data){
                var jsonData = data.data;
                for (var i = 0; i < jsonData.length; i++) {
                    jsonData[i].about = util.repalceHtml(jsonData[i].about);
                }
                oldData = oldData.concat(jsonData);
                that.setData({
                    detail: oldData,
                    readMoreFlag: jsonData.length > 9 ? true : false
                })
            }
        })
    },
    //上拉刷新
    onPullDownRefresh: function () {
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        this.setData({
            pageNumber: 1,
            detail:[]
        });
        this.getList();
    },
    //下拉加载
    onReachBottom: function () {
        var flag = this.data.readMoreFlag;
        if (flag) {
            var pageNum1 = this.data.pageNumber;
            // 显示加载图标  
            wx.showLoading({
                title: '玩命加载中',
            })
            this.setData({
                pageNumber: ++pageNum1,
            });
            this.getList();
        }
    },
    //书籍点赞
    dianzan: function (options) {
        var that = this;
        var index = options.currentTarget.dataset.index;
        var detail = this.data.detail[index];
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        util.wxAjax("glBook/updateBookLaudsAddOne", "post", {
            adminid: userInfo.smAdmin.id,
            id: detail.id
        }, function (res) {
            var data = res.data;
            if (data.result == 200 || data.reslut == "200") {
                wx.showToast({
                    title: '点赞成功！',
                })
                detail.lauds = data.data;
                detail.isLauds = 1;
                that.setData({
                    ["detail["+index+"]"]: detail
                });
            } else {
                wx.showToast({
                    title: data.msg,
                    icon: "none"
                })
            }
        });
    },
})